﻿using ScreenSaverDigitalClock.Properties;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ScreenSaverDigitalClock
{
    public partial class TimeScreenSaver : UserControl
    {
        public TimeScreenSaver()
        {
            InitializeComponent();
        }

        private void TimeScreenSaver_Load(object sender, EventArgs e)
        {
            InitControls();
        }

        private void TimeScreenSaver_SizeChanged(object sender, EventArgs e)
        {
            InitControls();
        }

        public void RefreshTime()
        {
            DateTime dateTime = DateTime.Now;
            hour.SetText(formatHMD(dateTime.Hour));
            minute.SetText(formatHMD(dateTime.Minute));
            month.SetText(((Months)dateTime.Month).ToString());
            day.SetText(formatHMD(dateTime.Day));
        }

        private void InitControls()
        {
            this.BackColor = Color.FromArgb(51, 51, 51);
            //Font fontHM = GetResoruceFont(Resources.Futura_LT_Bold, this.ClientSize.Width / 7);
            //Font fontMD = GetResoruceFont(Resources.Futura_LT_Bold, this.ClientSize.Width / 28);
            Font fontHM = new Font("黑体", this.ClientSize.Width / 7, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));
            Font fontMD = new Font("黑体", this.ClientSize.Width / 28, FontStyle.Bold, GraphicsUnit.Point, ((byte)(134)));

            hour.SetFont(fontHM);
            minute.SetFont(fontHM);
            month.SetFont(fontMD);
            day.SetFont(fontMD);
            this.Controls.Add(hour);
            this.Controls.Add(minute);
            this.Controls.Add(month);
            this.Controls.Add(day);
            RefreshTime();

            int sX = (this.ClientSize.Width - hour.Width - 20 - minute.Width - 20 - month.Width) / 2;  //  
            int sY = (this.ClientSize.Height - hour.Height) / 2;
            hour.Location = new Point(sX, sY);
            minute.Location = new Point(hour.Location.X + hour.Width + 20, hour.Location.Y);
            day.Location = new Point(minute.Location.X + minute.Width + 20, minute.Location.Y);
            day.SizeWH(month.Width, month.Height);
            month.Location = new Point(day.Location.X, day.Location.Y + day.Height + 20);
        }

        private string formatHMD(int iHMD)
        {
            string sHMD = Convert.ToString(iHMD);
            if(sHMD.Length < 2)
            {
                sHMD = "0" + sHMD;
            }
            return sHMD;
        }
        
        /// <summary>
        /// 如何使用资源文件中的字体，无安装无释放
        /// </summary>
        /// <param name="bytes">资源文件中的字体文件</param>
        /// <returns></returns>
        private Font GetResoruceFont(byte[] bytes, int fontSize)
        {
            System.Drawing.Text.PrivateFontCollection pfc = new System.Drawing.Text.PrivateFontCollection();
            IntPtr MeAdd = Marshal.AllocHGlobal(bytes.Length);
            Marshal.Copy(bytes, 0, MeAdd, bytes.Length);
            pfc.AddMemoryFont(MeAdd, bytes.Length);
            return new Font(pfc.Families[0], fontSize, FontStyle.Bold);
        }
    }
}
